#include "mystm.h"
#include "ui_mystm.h"

MyStm::MyStm(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::MyStm)
{
    ui->setupUi(this);
    data = new DataInfo;
    group = new QButtonGroup(this);
    group->addButton(ui->pushButton_ac,1);
    group->addButton(ui->pushButton_alarm,2);
    group->addButton(ui->pushButton_hum,3);
    group->addButton(ui->pushButton_zhe,4);
    connect(group,SIGNAL(buttonClicked(int)),this,SLOT(btnClickedSlot(int)));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(btnFlashSlot()));
}

MyStm::~MyStm()
{
    delete ui;
}

void MyStm::paintEvent(QPaintEvent *event)
{

    //创建画家类对象
    QPainter painter(this);
    QPixmap map(":/new/prefix1/res/1.JPG");
    painter.drawPixmap(0,0,this->width(),this->height(),map);

}

void MyStm::btnClickedSlot(int val)
{
    QJsonObject json;
    QJsonObject ctljson;
    json.insert("type",2);
    switch (val) {
    case 1:
        if(ui->pushButton_ac->text() == "关")
        {
            ui->pushButton_ac->setText("开");
            ctljson.insert("val","1");

        }else if(ui->pushButton_ac->text() == "开")
        {
            ui->pushButton_ac->setText("关");
            ctljson.insert("val","0");
        }
        ctljson.insert("key",205);
        break;
    case 2:
        if(ui->pushButton_alarm->text() == "关")
        {
            ui->pushButton_alarm->setText("开");
            ctljson.insert("val","1");

        }else if(ui->pushButton_alarm->text() == "开")
        {
            ui->pushButton_alarm->setText("关");
            ctljson.insert("val","0");
        }
        ctljson.insert("key",206);
        break;
    case 3:
        if(ui->pushButton_hum->text() == "关")
        {
            ui->pushButton_hum->setText("开");
            ctljson.insert("val","1");

        }else if(ui->pushButton_hum->text() == "开")
        {
            ui->pushButton_hum->setText("关");
            ctljson.insert("val","0");
        }
        ctljson.insert("key",204);
        break;
    case 4:
        if(ui->pushButton_zhe->text() == "关")
        {
            ui->pushButton_zhe->setText("开");
            ctljson.insert("val","1");

        }else if(ui->pushButton_zhe->text() == "开")
        {
            ui->pushButton_zhe->setText("关");
            ctljson.insert("val","0");
        }
        ctljson.insert("key",203);
        break;
    }
    json.insert("data",ctljson);
    data->publishMessage(json);

}

void MyStm::btnFlashSlot()
{

    QJsonObject json;
    json.insert("type",1);
    data->publishMessage(json);

}




void MyStm::keyValueGetSlot(int key, QString val)
{
    switch (key)
    {
    case 200:
        ui->label_in_tem->setText(val);
        break;
    case 201:
        ui->label_in_hum->setText(val);
        break;
    case 206:
        if(val == "0")
        {
            ui->pushButton_alarm->setText("关");
        }
        else if(val == "1")
        {
            ui->pushButton_alarm->setText("开");
        }
        break;
    case 204:
        if(val == "0")
        {
            ui->pushButton_hum->setText("关");
        }
        else if(val == "1")
        {
            ui->pushButton_hum->setText("开");
        }
        break;
    case 202:
        ui->label_bat->setText(val);
        break;
    case 203:
        if(val == "0")
        {
            ui->pushButton_zhe->setText("关");
        }
        else if(val == "1")
        {
            ui->pushButton_zhe->setText("开");
        }
        break;
    case 305:
        ui->label_ac->setText(val);
        break;
    case 205:
        if(val == "0")
        {
            ui->pushButton_ac->setText("关");
        }
        else if(val == "1")
        {
            ui->pushButton_ac->setText("开");
        }
        break;

    }
}
